package com.wyx.controller;


import com.wyx.entity.Menu;
import com.wyx.entity.ResultEntity;
import com.wyx.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 菜单管理 前端控制器
 * </p>
 *
 * @author wyx
 * @since 2022-03-17
 */
@RestController
@RequestMapping("/resource")
public class MenuController {

    @Autowired
    private MenuService menuService;
    //toDO 获取动态获取user标记
    @GetMapping("/showUserMenu")
    public ResultEntity showUserMenu(){
        int a=2;
        List<Menu> menus = menuService.selectAll(a);
        return ResultEntity.ok(menus);
    }

    @GetMapping("/showAllResourceTree")
    public ResultEntity showAllResourceTree(){
        List<Menu> menus = menuService.list();
        List<Menu> collect = menus.stream().filter((m) -> {return m.getType() == 0;})
                .peek((m) -> {
            m.setChildren(getChildren(m.getMenuId(), menus));
        }).collect(Collectors.toList());
        return ResultEntity.ok(collect);
    }
    private List<Menu> getChildren(Long mid,List<Menu> menuList){
        return menuList.stream().filter((m)->{return m.getParentId().equals(mid);})
                .peek((m)->{m.setChildren(getChildren(m.getMenuId(),menuList));})
                .collect(Collectors.toList());
    }
}

